//获取计算验证码
function getCalCode(){
  fetch('/login', {
    method: 'GET',
    credentials: 'include'
  }).then(res => res.json()).then(res => {
    console.log(res)
  })
}
//getCalCode();


//登录
function login (data){
  console.log(data)
  // fetch('/login', {
  //   method: 'POST',
  //   credentials: 'include',
  //   // headers: {
  //   //   "Content-Type": "application/json"
  //   // },
  //   body: data
  // }).then(res=>res.json()).then(res => {
  //   console.log(res)
  //   console.log();
  // })
  $.ajax({
    type: 'POST',
    url: '/login',
    data: data,
    xhrFields: {
      withCredentials: true
    },
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    success: function(response){
      if(response.status === '0'){
        location.href = 'content.html';
      }else if( response.status === '-1'){
        alert(response.message);
      }
    }
  })
}

$('#loginBtn').click(function(){
  let username = $('#username').val();
  let pwd = $('#pwd').val();
  let code = $('#code').val();
  if(!username){
    alert("用户名不能为空");
  } else if(!pwd){
    alert("密码不能为空");
  } else if(!code){
    alert('验证码不能为空');
  } else {
    login({username, pwd, code});
  }
})
